package com.hschinese.pronunciation.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.hschinese.pronunciation.activity.MyApplication;


@SuppressLint("CommitPrefEdits")
public class UserInfoUtil {
	private static UserInfoUtil userInfoUtil = null;
	private SharedPreferences settings;
//	private String vipRole;
	private long endTime = -1l;
	
	private UserInfoUtil(Context ctx){
		settings = ctx.getSharedPreferences("user_info", 0);
	}
	
	public static UserInfoUtil getInstance(Context ctx){
		if(null == userInfoUtil){
			synchronized (UserInfoUtil.class) {
				if(null == userInfoUtil){
					userInfoUtil = new UserInfoUtil(ctx);
				}
			}
		}
		return userInfoUtil;
	}
	
	public String getEmail(){
		return settings.getString(Constant.EMAIL, null);
	}
	
	public boolean getSignStatus(){
		return settings.getBoolean(Constant.SIGN_IN_STATUS,false);
	}
	
	public void setSignStatus(boolean signStatus){
		Editor editor = settings.edit();
		editor.putBoolean(Constant.SIGN_IN_STATUS, signStatus);
		editor.commit();
	}
	
	public String getPicture(){
		return settings.getString(Constant.PICTURE, null);
	}
	
	public String getNikeName(){
		return settings.getString(Constant.NIKENAME, null);
	}
	
	public String getUid(){
		return settings.getString(Constant.UID,null);
	}
	
	public int getIsGuest(){
		return settings.getInt(Constant.ISGUEST, 0);    //游客为0   登录用户为1
	}
	
	public String getRole(){
		String role = settings.getString(Constant.ROLE, null);
		return role;
	}
	
	public Long getEndTime(){
		return settings.getLong(Constant.ENDTIME, 0);
	}
	
	public long getGoldNum(){
		return settings.getLong(Constant.GOLDNUM, 0);
	}
	
	public void setGoldNum(long goldNum){
		Editor editor = settings.edit();
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.commit();
	}
	
	
	public void setVip(String role,long endTime,long goldNum){
//		this.vipRole = role;
		this.endTime = endTime;
		Editor editor = settings.edit();
		editor.putString(Constant.ROLE, role);
		editor.putLong(Constant.ENDTIME, endTime);
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.commit();
	}
	
	public void clear(){
		settings.edit().clear().commit();
	}
	
	public void setUserInfo(String uid,String email,String nikeName,String picture,boolean signStatus,int isGuest,long goldNum){
		Editor editor = settings.edit();
		editor.putString(Constant.UID, uid);
		editor.putString(Constant.EMAIL, email);
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putBoolean(Constant.SIGN_IN_STATUS, signStatus);
		editor.putInt(Constant.ISGUEST, isGuest);
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.commit();
	}
	
	
	public void setUserInfo(String uid,String email,String nikeName,String picture,boolean signStatus,int isGuest){
		Editor editor = settings.edit();
		editor.putString(Constant.UID, uid);
		editor.putString(Constant.EMAIL, email);
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putBoolean(Constant.SIGN_IN_STATUS, signStatus);
		editor.putInt(Constant.ISGUEST, isGuest);
		editor.commit();
	}
	
	public void setUserInfo(String uid,String email,String nikeName,String picture,boolean signStatus,int isGuest,long goldNum,String role,Long endTime){
		Editor editor = settings.edit();
		editor.putString(Constant.UID, uid);
		editor.putString(Constant.EMAIL, email);
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putBoolean(Constant.SIGN_IN_STATUS, signStatus);
		editor.putInt(Constant.ISGUEST, isGuest);
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.putString(Constant.ROLE, role);
		editor.putLong(Constant.ENDTIME, endTime);
		editor.commit();
	}
	
	public void setUserInfo(String uid,String email,String nikeName,String picture,boolean signStatus,int isGuest,String role,Long endTime){
		Editor editor = settings.edit();
		editor.putString(Constant.UID, uid);
		editor.putString(Constant.EMAIL, email);
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putBoolean(Constant.SIGN_IN_STATUS, signStatus);
		editor.putInt(Constant.ISGUEST, isGuest);
		editor.putString(Constant.ROLE, role);
		editor.putLong(Constant.ENDTIME, endTime);
		editor.commit();
	}
	
	public void setUserInfo(String nikeName,String picture,long goldNum,String role,Long endTime){
		Editor editor = settings.edit();
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.putString(Constant.ROLE, role);
		editor.putLong(Constant.ENDTIME, endTime);
		editor.commit();
	}
	
	public void setUserInfo(String nikeName,String picture,long goldNum){
		Editor editor = settings.edit();
		editor.putString(Constant.NIKENAME, nikeName);
		editor.putString(Constant.PICTURE, picture);
		editor.putLong(Constant.GOLDNUM, goldNum);
		editor.commit();
	}
	
	public void setExitStatus(){
		Editor editor = settings.edit();
		editor.remove(Constant.UID);
		editor.remove(Constant.NIKENAME);
		editor.remove(Constant.PICTURE);
		editor.remove(Constant.SIGN_IN_STATUS);
		editor.remove(Constant.GOLDNUM);
		editor.remove(Constant.ROLE);
		editor.remove(Constant.ENDTIME);
		endTime = -1;
		MyApplication.getInstance().setUid(null);
		MyApplication.getInstance().setIsGuest(-1);
		if(0 == getIsGuest()){
			editor.remove(Constant.EMAIL);
		}
		editor.commit();
	}
	
	public void setEndTimeNull(){
		endTime = -1;
	}
	
	public long getVipEndTime(){
		if(-1 == endTime){
			endTime = getEndTime();
		}
		return endTime;
	}
	
	@SuppressLint("DefaultLocale")
	public boolean getVipStatusResult(){
		boolean isVip = false;
		String role = getRole();
		if(getVipEndTime() > Utils.getTimestamp() && !TextUtils.isEmpty(role) && role.toLowerCase().contains("vip")){
			isVip = true;
		}
		return isVip;
	}
}
